//
//  HNSH_GoodsListTopView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/24.
//

#import "HNSH_GoodsListTopView.h"

@interface HNSH_GoodsListTopView ()

/* buttons */
@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation HNSH_GoodsListTopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor appWhiteLabelColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    NSArray *titles = @[@"综合",@"销量",@"最新",@"价格"];
    CGFloat width = (kScreenWidth)/titles.count;
    self.buttons = [NSMutableArray arrayWithCapacity:titles.count];
    for (int i = 0; i < titles.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width*i, 0, width, 48);
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor appGrayLabelColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor appGreenColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = i+1;
        if (button.tag != 1 && button.tag != 3) {
            [button setImage:[UIImage imageNamed:@"base_goods_px_del"] forState:UIControlStateNormal];
            [button HNSH_layoutButtonWithEdgeInsetsStyle:HHZXButtonStyleTextLeft imageTitleSpace:7];
        }
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [self.buttons addObject:button];
    }
}

- (void)buttonClick:(UIButton *)sender {
    for (int i = 0; i < self.buttons.count; i ++) {
        UIButton *button = self.buttons[i];
        if (button.tag != sender.tag) {
            button.selected = NO;
            button.tag = i + 1;
            if (button.tag != 3) {
                [button setImage:[UIImage imageNamed:@"base_goods_px_del"] forState:UIControlStateNormal];
            }
        }
    }
    if (sender.tag != 1 && sender.tag != 3) {
        if (sender.isSelected) {
            if (sender.tag >= 10) {
                sender.tag = sender.tag / 10;
                [sender setImage:[UIImage imageNamed:@"base_goods_px_xia"] forState:UIControlStateNormal];
            } else {
                sender.tag = sender.tag * 10;
                [sender setImage:[UIImage imageNamed:@"base_goods_px_shang"] forState:UIControlStateNormal];
            }
        } else {
            sender.selected = YES;
            [sender setImage:[UIImage imageNamed:@"base_goods_px_xia"] forState:UIControlStateNormal];
        }
    } else {
        sender.selected = YES;
    }
    
    NSLog(@"sender.tag ==> %ld",sender.tag);
    if (self.typeBlock) {
        self.typeBlock(sender.tag);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
